//
//  SISSPassengerCooperationProgressManageViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/5/17.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSPassengerCooperationProgressManageViewController.h"
#import "SISSPassengerCooperationProgressManageCell.h"
#import "SISSComboBoxView.h"
#import "SISSJsonRequestNetworkingManager+Progress.h"
#import "SISSChatViewController.h"
#import "SISSPassengerCooperationProgressViewController.h"

static NSString *passengerCooperationProgressManageCellReuseID = @"passengerCooperationProgressManageCellReuseID";
@interface SISSPassengerCooperationProgressManageViewController ()<SISSComboBoxViewDelegate,UITableViewDataSource,UITableViewDelegate,SISSPassengerCooperationProgressManageCellDelegate>


/** tableView */
@property (weak, nonatomic) SISSBaseTableView *tableView;
/* 客源合作进度模型数组 */
@property (strong, nonatomic) NSArray<SISSPassengerCoopProgressModel *> *passengerCooProgressArr;

/** 区域 */
@property (copy, nonatomic) NSString *region;
/** 街道办 */
@property (copy, nonatomic) NSString *regionDetail;
/** 价格 */
@property (copy, nonatomic) NSString *price;
/** 户型 */
@property (copy, nonatomic) NSString *houseType;
/** 面积 */
@property (copy, nonatomic) NSArray *area;
/** 朝向 */
@property (copy, nonatomic) NSArray *direction;
/** 装修程度 */
@property (copy, nonatomic) NSArray *decoration;
/** 合作范围 */
@property (copy, nonatomic) NSArray *cooperationRange;
/** 合作身份 */
@property (copy, nonatomic) NSArray *cooperationStatus;


@end

@implementation SISSPassengerCooperationProgressManageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    [self requestPassengerCooperationProgressManageData];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    WeakSelf
    [kSISSRefresh prepareHeaderRefresh:self.tableView loadDataBlock:^{
        kSISSRefresh.refreshType = SISSRefressTypeUpDown;
        [weakSelf upDownLoadData];
    }];
    
//    [kSISSRefresh prepareFooterRefresh:self.tableView reloadDataBlock:^{
//        kSISSRefresh.refreshType = SISSRefressTypeReloadMore;
//        [weakSelf reloadMoreData];
//    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
//    [self requestPassengerCooperationProgressManageData];
}


#pragma mark - SISSComboBoxViewDelegate
- (void)comboBoxViewDidSelectDisplayTypeNormalWithRootItemTitle:(NSString *)rootItemTitle title:(NSString *)title {
    
        if ([rootItemTitle isEqualToString:@"预算"]) {
            self.price = title;
        } else if ([rootItemTitle isEqualToString:@"户型"]) {
            self.houseType = title;
        }
        [self requestPassengerCooperationProgressManageData];
}

- (void)comboBoxViewDidSelectDisplayTypeMultilayerWithRootItemTitle:(NSString *)rootItemTitle title:(NSString *)title subtitle:(NSString *)subtitle {
    
        self.region = title;
        self.regionDetail = subtitle;
        [self requestPassengerCooperationProgressManageData];
}

- (void)comboBoxViewDidSelectDisplayTypeFiltersWithTitle:(NSString *)title subtitles:(NSArray *)subtitles {
    
    if ([title isEqualToString:@"合作身份"]) {
        self.cooperationStatus = @[];
        self.cooperationStatus = subtitles;
    }  else if ([title isEqualToString:@"合作范围"]) {
        self.cooperationRange = @[];
        self.cooperationRange = subtitles;
    } else if ([title isEqualToString:@"面积"]) {
        self.area = @[];
        self.area = subtitles;
    } else if ([title isEqualToString:@"朝向"]) {
        self.direction = @[];
        self.direction = subtitles;
    } else if ([title isEqualToString:@"装修程度"]) {
        self.decoration = @[];
        self.decoration = subtitles;
    }
    
}

- (void)comboBoxViewDisplayTypeFiltersConfirmClickAction {
    
    [self requestPassengerCooperationProgressManageData];
    
    self.cooperationStatus = @[];
    self.cooperationRange = @[];
    self.area = @[];
    self.direction = @[];
    self.decoration = @[];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    [tableView tableViewDisplayWithMessage:@"暂无数据" ifNecessaryForRowCount:self.passengerCooProgressArr.count];
    return self.passengerCooProgressArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SISSPassengerCooperationProgressManageCell *cell = [tableView dequeueReusableCellWithIdentifier:passengerCooperationProgressManageCellReuseID forIndexPath:indexPath];
    cell.passengerCoopProgressModel = self.passengerCooProgressArr[indexPath.row];
    cell.delegate = self;
    return cell;
    
}

#pragma mark - SISSPassengerCooperationProgressManageCellDelegate
- (void)passengerCooperationProgressManageCell:(SISSPassengerCooperationProgressManageCell *)cell clickContactButton:(UIButton *)button {
    
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    
    SISSChatViewController *chatVC = [[SISSChatViewController alloc] initWithConversationChatter:self.passengerCooProgressArr[indexPath.row].imAccount conversationType:EMConversationTypeChat];
    SISSContactModel *model = [[SISSContactModel alloc] init];
    model.name = self.passengerCooProgressArr[indexPath.row].name;
    model.portrait = self.passengerCooProgressArr[indexPath.row].portrait;
    model.spId = self.passengerCooProgressArr[indexPath.row].sp_id;
    model.accountType = 1;
    chatVC.contactModel = model;
    chatVC.title = self.passengerCooProgressArr[indexPath.row].name;
    [self.navigationController pushViewController:chatVC animated:YES];
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SISSPassengerCooperationProgressViewController *vc = [[SISSPassengerCooperationProgressViewController alloc] init];
    vc.title = @"合作进度";
    vc.passengerCoopProgressModel = self.passengerCooProgressArr[indexPath.row];
    WeakSelf
    vc.deleteCompletedBlock = ^{
        
        [weakSelf requestPassengerCooperationProgressManageData];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 上下拉刷新加载方法
- (void)upDownLoadData {
    
    //    self.pageNo = 1;
    
    [self requestPassengerCooperationProgressManageData];
    
}

- (void)reloadMoreData {
    
    //    self.pageNo += 1;
    
    [self requestPassengerCooperationProgressManageData];
}

#pragma mark - 加载数据
- (void)requestPassengerCooperationProgressManageData {
    
    NSString *region = self.region ? self.region : @"不限";
    NSString *regionDetail = self.regionDetail ? self.regionDetail : @"不限";
    NSString *price = self.price ? self.price : @"";
    NSString *houseType = self.houseType ? self.houseType : @"不限";
    
    NSDictionary *parameters = @{
                                 @"spId":[SISSUserAccount shared].spId,
                                 @"soId":[SISSUserAccount shared].soId,
                                 @"sobId":[SISSUserAccount shared].sobId,
                                 @"region":region,
                                 @"regionDetail":regionDetail,
                                 @"price":price,
                                 @"houseType":houseType,
                                 @"area":self.area,
                                 @"direction":self.direction,
                                 @"decoration":self.decoration,
                                 @"identity":self.cooperationStatus,
                                 @"cooperation_range":self.cooperationRange,
                                 };
    WeakSelf
    [kJsonRequestNetworkingManager requestFilterPassengerCooperationProgressWithParameters:parameters success:^(NSArray<SISSPassengerCoopProgressModel *> *passengerCooperationArr) {
        
        weakSelf.passengerCooProgressArr = passengerCooperationArr;
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            
            [weakSelf.tableView reloadData];
            [weakSelf.tableView.mj_header endRefreshing];
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
        }];
        
    } failed:^(NSError *error) {
        [weakSelf.tableView.mj_header endRefreshing];
        NSLog(@"%@",error);
    }];
}

#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    SISSComboBoxView *filterView = [[SISSComboBoxView alloc] initWithFrame:CGRectMake(0, self.navigationHeight, SCREEN_WIDTH, 44) isHouseResource:NO isCooperation:YES isProgress:YES];
    filterView.delegate = self;
    
    [self.view addSubview:filterView];
    self.filterView = filterView;
    
    SISSBaseTableView *tableView = [[SISSBaseTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    tableView.dataSource = self;
    tableView.delegate = self;
    // 自动行高
    tableView.estimatedRowHeight = 100;
    tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [tableView registerNib:[UINib nibWithNibName:@"SISSPassengerCooperationProgressManageCell" bundle:nil] forCellReuseIdentifier:passengerCooperationProgressManageCellReuseID];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(filterView.mas_bottom);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-kTabBarHeight);
    }];
}


#pragma mark - 懒加载

- (NSArray *)area {
    if (_area == nil) {
        _area = [NSArray array];
    }
    return _area;
}

- (NSArray *)direction {
    if (_direction == nil) {
        _direction = [NSArray array];
    }
    return _direction;
}

- (NSArray *)decoration {
    if (_decoration == nil) {
        _decoration = [NSArray array];
    }
    return _decoration;
}

- (NSArray *)cooperationRange {
    if (!_cooperationRange) {
        _cooperationRange = [NSArray array];
    }
    return _cooperationRange;
}

- (NSArray *)cooperationStatus {
    if (!_cooperationStatus) {
        _cooperationStatus = [NSArray array];
    }
    return _cooperationStatus;
}

@end
